/**
 * @file dateUtils.ts
 * @description 日期工具类
 * @module utils/dateUtils
 * @author kikolove
 */

export default class DateUtils {
    static getNowDate(format: string = 'yyyy-MM-dd HH:mm:ss') {
        const date = new Date();
        return this.formatDate(date, format);
    }

    static formatDate(date: Date, format: string = 'yyyy-MM-dd HH:mm:ss') {
        const year = date.getFullYear();
        const month = date.getMonth() + 1;
        const day = date.getDate();
        const hour24 = date.getHours();
        const hour12 = hour24 % 12 === 0 ? 12 : hour24 % 12;
        const minute = date.getMinutes();
        const second = date.getSeconds();
        const formatMap: { [key: string]: any } = {
            yyyy: year.toString(),
            MM: month.toString().padStart(2, '0'),
            dd: day.toString().padStart(2, '0'),
            HH: hour24.toString().padStart(2, '0'),
            hh: hour12.toString().padStart(2, '0'),
            mm: minute.toString().padStart(2, '0'),
            ss: second.toString().padStart(2, '0')
        };
        return format.replace(/yyyy|MM|dd|HH|hh|mm|ss/gi, (match) => formatMap[match]);
    }

    static formatTimestampToDate(timestamp: number, format: string): string {
        const date = new Date(timestamp);
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        const formattedDate = format.replace(/YYYY/i, String(year))
            .replace(/MM/i, month)
            .replace(/DD/i, day)
            .replace(/HH/i, hours)
            .replace(/mm/i, minutes)
            .replace(/ss/i, seconds);
        return formattedDate;
    }
}